package main

import (
	"database/sql"
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
	_ "github.com/gogf/gf/contrib/drivers/v2/sqlite"
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/os/gfile"
	"github.com/gogf/gf/v2/util/gconv"
	octicons "github.com/lusingander/fyne-octicons"
	"github.com/y2h/fyne_poem/dbb"
	"github.com/y2h/fyne_poem/menu"
	"github.com/y2h/fyne_poem/util"
	"github.com/y2h/fyne_poem/widgets"
)

var tbls []string
var currentTbl string

var db *sql.DB
var newDb gdb.DB

// 在界面底部显示操作信息
var msg *widget.Label
var tabs *container.DocTabs
var tblList binding.StringList
var dbpath binding.String

var w fyne.Window
var a fyne.App

func main() {
	a = app.NewWithID("io.fyne.demo")
	a.Settings().SetTheme(&myTheme{})
	a.SetIcon(octicons.DatabaseIcon())
	w = a.NewWindow("Database Manager")
	tblList = binding.NewStringList()
	dbpath = binding.NewString()

	dbpath.Set("./quantangshi.db")
	menu.SetGlog()
	msg = widget.NewLabel("")
	tabs = container.NewDocTabs()
	tabs.SetTabLocation(container.TabLocationBottom)

	defer func() {
		if db != nil {
			db.Close()
		}
		if newDb != nil {
			newDb.Close(newDb.GetCtx())
		}
	}()
	// widgets.Treee=widget.NewTree(nil,nil,nil,nil)
	// widgets.Treee = widget.NewTreeWithStrings(data)
	widgets.Treee = widget.NewTree(func(tni widget.TreeNodeID) []widget.TreeNodeID {
		return util.Data[tni]
	}, func(tni widget.TreeNodeID) bool {
		_, b := util.Data[tni]
		return b
	}, func(b bool) fyne.CanvasObject {
		return widgets.NewClickLbl("Template Object", nil, nil)
	}, func(tni widget.TreeNodeID, b bool, co fyne.CanvasObject) {
		lbl := co.(*widgets.Clicklbl)
		v, found := garray.NewStrArrayFrom(strings.Split(tni, "/")).PopRight()
		if found {
			lbl.SetText(v)
		}
		// lbl.SetText(tni)
		lbl.OnDoubleTapped = func() {
			widgets.Treee.Select(tni)
			if !util.IsInSlice(tni, tbls) {
				return
			}
			dataBrowser := container.NewTabItemWithIcon("",
				octicons.NoteIcon(),
				widgets.Tbs(newDb, tni, msg, w, a))

			dataBrowser.Text = tni + "(" +
				gfile.Name(newDb.GetConfig().Name) + ")"
			tabs.Append(dataBrowser)
			tabs.Select(dataBrowser)
		}
		lbl.OnTapped = func() {
			widgets.Treee.Select(tni)
		}
	})
	widgets.Treee.OnSelected = func(uid widget.TreeNodeID) {
		if util.IsInSlice(uid, tbls) {
			count, _ := newDb.Model(uid).Count()
			msg.SetText("共" + gconv.String(count) + "条纪录")
			currentTbl = uid
			//	widgets.Treee.ToggleBranch(uid)
			//widgets.Treee.Unselect(uid)
		} else {
			msg.SetText(uid)
		}
	}
	cc := container.NewHSplit(widgets.Treee, tabs)
	cc.Offset = 0.2
	tb := widget.NewToolbar(
		// display the created sql string of the table
		widget.NewToolbarAction(octicons.NoteIcon(), func() {
			// Ddl()
		}),
		// browse table data
		widget.NewToolbarAction(theme.GridIcon(), func() {
			// getData(newDb, currentTbl, a)
		}),
		widget.NewToolbarAction(theme.InfoIcon(), func() {
			dbb.GenStruct(newDb, currentTbl, tabs, msg)
		}),
	)

	btn := widget.NewButton("Connect", func() {
		connect()
	})
	top := container.NewVBox(tb,
		container.NewBorder(nil, nil, nil, btn, widgets.NewOpenFile(dbpath, w)),
	)

	c := container.NewBorder(top, msg, nil, nil, cc)
	w.SetContent(c)
	w.SetMainMenu(menu.MainMenuu(a))
	w.SetMaster()
	w.SetCloseIntercept(func() {
		dialog.ShowConfirm("Closing...", "Are you sure to close?", func(b bool) {
			if b {
				w.Close()
			}
		}, w)
	})
	w.Resize(fyne.NewSize(800, 700))
	w.ShowAndRun()
}
